﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using System.Media;
using System.Threading;
using System.Windows.Media.Effects;

namespace Hugo
{
    class MemoryGamePage : PlayGamesPage
    {
        private List<Button> playlist;
        private List<string> colorsList;
        private List<SolidColorBrush> colorBrushes;

        private ImageBrush bkBrush;

        public MemoryGamePage()
        {
            //Insert code required on object creation below this point.
            labelTitle.Content = Translate.translate("");

            colorBrushes = new List<SolidColorBrush> { Brushes.Magenta, Brushes.MediumBlue, Brushes.Yellow, Brushes.ForestGreen, Brushes.Red, Brushes.Turquoise };
            colorsList = new List<string> { "pink", "blue", "yellow", "green", "red", "turquoise" };

            bkBrush = new ImageBrush();
            bkBrush.ImageSource = Util.getBitmapSource(Properties.Resources.smiley);

            InitializeElements();
            SetRandomColors();

        }

    
        private void InitializeElements()
        {
            elements = new List<Button>();
            playlist = new List<Button>();

            int xPos = 300;
            int yPos = 125;

            for (int i = 0; i < 12; i++)
            {
                Button newButton = new Button();
                newButton.Name = "Button" + i;
                this.RegisterName(newButton.Name, newButton);
                newButton.Height = 130;
                newButton.Width = 200;
                newButton.HorizontalAlignment = HorizontalAlignment.Left;
                newButton.BorderThickness = new Thickness(0);
                newButton.Style = (Style)FindResource(ToolBar.ButtonStyleKey);

                Canvas.SetLeft(newButton, xPos);
                Canvas.SetTop(newButton, yPos);

                xPos = xPos + 250;
                if (i == 3 || i == 7)
                {
                    xPos = 300;
                    yPos = yPos + 180;
                }


                newButton.Click += new RoutedEventHandler(element_Click);

                elements.Add(newButton);
                playlist.Add(newButton);
                mainCanvas.Children.Add(newButton);
            }


        }

        
        private void SetRandomColors()
        {
            List<String> indexList = new List<String>();
            int index = 0;
            Random randomColor = new Random();
            foreach (Button button in elements)
            {
                do
                    index = randomColor.Next(0, 6);
                while (((indexList.FindAll(l => l == colorsList[index])).Count > 1) && indexList.Count < elements.Count);
                button.Background = bkBrush;
                button.Tag = colorBrushes[index];
                indexList.Add(colorsList[index]);
                button.IsEnabled = true;
            }
        }

        private void CheckIfEndOfGame()
        {
            foreach (Button button in elements)
            {
                if (button.IsEnabled == true)
                    return;
            }
            win.Play();
            Thread.Sleep(500);
            SetRandomColors();
        }

        void element_Click(object sender, RoutedEventArgs e)
        {
            //beep.Play();
            Thread.Sleep(400);

            int count = 0;
            foreach (Button button in playlist)
            {
                if (button.Background != bkBrush && button.IsEnabled)
                    count++;
                if (count == 2)
                    break;
            }
            if (count == 2)
            {
                foreach (Button button in playlist)
                {
                    if (button.IsEnabled)
                        button.Background = bkBrush;
                }
            }

            Button target = (Button)e.Source;
            target.Background = (Brush)target.Tag;
            foreach (Button button in playlist)
            {
                if (button != target && button.Background != bkBrush && button.IsEnabled)
                {

                    if (button.Background == target.Background)
                    {
                        Random randomSound = new Random();
                        int index = randomSound.Next(0, 4);
                        //match[index].Play();

                        button.IsEnabled = false;
                        target.IsEnabled = false;
                    }
                    else
                    {
                        //miss.Play();
                    }
                    break;

                }

            }
            CheckIfEndOfGame();
        }
    }
}
